package com.wjx.test;

import com.wjx.entity.User;
import com.wjx.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CRUDTest {

    private SqlSession session;

    @Before
    public void init(){
        //利用MyBatisUtil工具类获取数据库的连接
        session = MyBatisUtil.openSession();
    }

    //查询数据
    @Test
    public void testSelection(){
        try {
            int rows = session.selectOne("usermp.findUserCount");
            System.out.println(rows);
            session.commit();
        } catch (Exception e) {
            System.out.println("rollback");
            session.rollback();
        }

    }

    //插入数据
    @Test
    public void testInsertUser(){
        System.out.println("start");
        try {
            System.out.println("1");
            User user1 = new User("user7", "123456");
            int rows = session.insert("usermp.insertUser", user1);
            System.out.println(rows);
            session.commit();
        } catch (Exception e) {
            System.out.println("rollback");
            session.rollback();
        }

    }

    @After
    public void destory(){
        //关闭连接
        session.close();
    }
}



